home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / CAD / LISP04.ARJ / LLOAD.LSP < prev    next >
Lisp/Scheme  |  1990-10-12  |  17KB  |  572 lines

  1. ;;;   LLoad.lsp
  2. ;;;   Copyright (C) 1990 by Autodesk, Inc.
  3. ;;;  
  4. ;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  5. ;;;   ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  6. ;;;   MERCHANTABILITY ARE HEREBY DISCLAIMED.
  7. ;;; 
  8. ;;;   by Jan S. Yoder
  9. ;;;   01 February 1990
  10. ;;;
  11. ;;;--------------------------------------------------------------------------;
  12. ;;; DESCRIPTION
  13. ;;;   
  14. ;;;   This routine allows you to create a list of AutoLisp file names that
  15. ;;;   you load frequently, and allows you to load any of them by typing 
  16. ;;;   the number associated with the file name.  This file name can be any
  17. ;;;   legal pathname with d4rive letters, etc. that is acceptable to the 
  18. ;;;   platform or machine on which AutoCAD is running.  This can be very
  19. ;;;   helpful in a networking situation where the file you wish to load is
  20. ;;;   on a path such as n:\acad\ourstuff\lsp\etc\foo.lsp.  Typing
  21. ;;;   
  22. ;;;     (load "n:\acad\ourstuff\lsp\etc\foo")
  23. ;;;     
  24. ;;;   with the correct syntax is something best left alone.
  25. ;;;   
  26. ;;;   By using Lload, you can reduce the number of times that you need to 
  27. ;;;   type long path names, and remember the exact syntax to a single time,
  28. ;;;   and you don't even need to remember the syntax.
  29. ;;;   
  30. ;;;   LLoad
  31. ;;;   
  32. ;;;   The first time you run Lload.lsp, you will be asked whether or not you
  33. ;;;   want a default file built.  If you answer No, then you can type the
  34. ;;;   name of a file you want loaded.  However, if you answer Yes, a new,
  35. ;;;   blank file called lload.dfs is created for you, and you may begin 
  36. ;;;   adding file names to it.
  37. ;;;   
  38. ;;;     Build a new default file?  <Y>: 
  39. ;;;   
  40. ;;;     LispLoad  Version 1.00
  41. ;;;     Available Lisp files: 
  42. ;;;   
  43. ;;;   
  44. ;;;     Add/Remove an entry/<Number to load>:   (a)
  45. ;;;     Lisp routine name to load <No default>:
  46. ;;;           
  47. ;;;   Type Add to add a file name.  When you do this, the routine checks to
  48. ;;;   see that the file does exist, and if it does, it is loaded into 
  49. ;;;   memory and added to the list.  The list is then displayed again, and
  50. ;;;   you are prompted as before.  You may add as many routines to the list 
  51. ;;;   as you wnat, as long as AutoCAD has the memory to load them. 
  52. ;;;   
  53. ;;;   You may also remove items from the menu by typing the number associated 
  54. ;;;   with it.  However, this does not remove the routine from memory;  you
  55. ;;;   must leave the current AutoCAD drawing session to do that.
  56. ;;;   
  57. ;;;     Number of entry to remove from list:
  58. ;;;   
  59. ;;;   After you have several items in the list, you may load or reload the 
  60. ;;;   routine simply by typing its number.
  61. ;;;   
  62. ;;;   Pressing RETURN at the Add/Remove prompt exits you from the routine
  63. ;;;   without doing anything.
  64. ;;;   
  65. ;;;   
  66. ;;;   XLoad/XULoad
  67. ;;;   
  68. ;;;   There is a parallel routine called XLoad which allows you to maintain
  69. ;;;   a similar list of external functions written in ADS.  The prompts and
  70. ;;;   structure are the same.  XULoad allows you to unload ADS functions
  71. ;;;   from the same list.
  72. ;;;
  73. ;;;
  74. ;;;--------------------------------------------------------------------------;
  75. ;;;
  76. ;;; Function main
  77. ;;;
  78. (defun l_load (xld unload / a ll_ver ll_oe ll_oer ll_err ll_oc xld deffi I_LIST)
  79.  
  80.   (setq ll_ver "1.00b")               ; Reset this local if you make a change.
  81.   (setq ll_xpf (ll_cpf (getvar "acadprefix" )))
  82.   (setq ll_llf "lload.dfs")           ; Reset this local if you make a change.
  83.   (setq ll_xlf "xload.dfs")           ; Reset this local if you make a change.
  84.   
  85.   (if ll_err                          ; Set our new error handler
  86.     (setq ll_oer ll_err) 
  87.   )
  88.   ;;
  89.   ;; Internal error handler defined locally
  90.   ;;
  91.  
  92.   (defun ll_err (s)                   ; If an error (such as CTRL-C) occurs
  93.                                       ; while this command is active...
  94.     (if (/= s "Function cancelled")
  95.       (if (= s "quit / exit abort")
  96.         (princ)
  97.         (princ (strcat "\nError: " s))
  98.       )
  99.     )
  100.     (if deffi (setq deffi (close deffi)))
  101.     (command "undo" "end")
  102.     (if ll_oe                         ; If an old error routine exists
  103.       (setq *error* ll_oe)            ; then, reset it 
  104.     )
  105.     (setvar "cmdecho" ll_oc)          ; Reset command echoing on error
  106.     (princ)
  107.   )
  108.   
  109.   ;;
  110.   ;; Body of LLOAD function
  111.   ;;
  112.   
  113.   (if *error*                         ; Set our new error handler
  114.     (setq ll_oe *error* *error* ll_err) 
  115.     (setq *error* ll_err) 
  116.   )
  117.   (setq ll_oc (getvar "cmdecho"))     ; Save current state of command echoing
  118.   (setvar "cmdecho" 0)                ; Turn off command echoing
  119.   (command "undo" "group")            ; Start an UNDO group
  120.  
  121.   ;;
  122.   ;; Look for the default file.
  123.   ;;
  124.  
  125.   (setq deffi (ll_lfx (if xld ll_xlf ll_llf) "r"))
  126.  
  127.   ;;
  128.   ;; If found, then process, else create one and process
  129.   ;;
  130.  
  131.   (if deffi
  132.     (ll_gos)                          ; LLoad_Get_OptS
  133.     (progn
  134.       (ll_bdf)                        ; LLoad_Build_Default_File
  135.       (setq deffi (ll_lfx (if xld ll_xlf ll_llf) "r"))
  136.       (if deffi
  137.         (ll_gos)                      ; LLoad_Get_OptS
  138.         (progn
  139.           (princ "\n\tCouldn't open the default file for reading. ")
  140.           (exit)
  141.         )
  142.       )
  143.     )
  144.   )
  145.   (if deffi (setq deffi (close deffi)))
  146.   (command "undo" "end")              ; End the UNDO group
  147.   (if ll_oe                           ; If an old error routine exists
  148.     (setq *error* ll_oe)              ; then, reset it
  149.   )
  150.   (if ll_oer                          ; Reset the old error handler
  151.     (setq ll_err ll_oer) 
  152.   )
  153.   (setvar "cmdecho" ll_oc)            ; Reset command echoing
  154.   (princ)
  155. )
  156. ;;;
  157. ;;; Look for an external definition file in AutoCAD's search path
  158. ;;; ll_lfx == LLoad_Look_For_Xfile
  159. ;;;
  160. (defun ll_lfx (f_name r_or_w / lfile temp)
  161.   ;; Look for f_name in AutoCAD's search paths.
  162.   (if (= r_or_w "w")
  163.     (if (setq temp (open f_name r_or_w))
  164.       temp                            ; Return file descriptor
  165.       (progn
  166.         (princ (strcat "\n\tCouldn't open " f_name " for writing. "))
  167.         (exit)
  168.       )
  169.     )
  170.     (if (setq lfile (findfile f_name))
  171.       (if (setq temp (open lfile r_or_w))
  172.         temp                          ; Return file descriptor
  173.         (progn
  174.           (princ (strcat "\n\tCouldn't open " f_name " for reading. "))
  175.           (exit)
  176.         )
  177.       )
  178.       nil                             ; or nil
  179.     )
  180.   )
  181. )
  182. ;;;
  183. ;;; Get the user's options
  184. ;;; ll_gos == LLoad_Get_OptS
  185. ;;;
  186. (defun ll_gos (/ d_item max_ls ans)
  187.   (if textpage (textpage) (textscr))  ; For Release 10
  188.   (setq ans T)
  189.   (setq deffi (close deffi))
  190.   (while ans
  191.     ;;
  192.     ;; LLoad_Look_For_Xfile
  193.     ;;
  194.     (setq deffi (ll_lfx (if xld ll_xlf ll_llf) "r"))
  195.     (if (null deffi)
  196.       (setq ans nil)
  197.       (progn
  198.         (if xld
  199.           (ll_rux ";;; XLOAD Default Files" 1 23)
  200.           (ll_rux ";;; LISP Default Files" 1 22)
  201.         )
  202.         (if xld
  203.           (if unload
  204.             (princ (strcat "\n\tXUnLoad  Version " ll_ver 
  205.                            "\n\tAvailable ADS programs: \n"))
  206.             (princ (strcat "\n\tXLoad  Version " ll_ver 
  207.                            "\n\tAvailable ADS programs: \n"))
  208.           )
  209.           (princ (strcat "\n\tLispLoad  Version " ll_ver 
  210.                          "\n\tAvailable Lisp files: \n"))
  211.         )
  212.         (setq I_LIST nil)
  213.         (setq max_ls (ll_lns "" 1 1))
  214.         (setq ans (strcat 
  215.           "\n\n\tAdd/Remove an entry/<Number to " (if unload "un" "") "load>: "))
  216.  
  217.         (setq deffi (close deffi))
  218.  
  219.         (setq d_item (ll_pfl max_ls 6 "Add Remove" ans))
  220.         (cond
  221.           ((= d_item nil)
  222.             ;; No file selected.  Exiting. 
  223.             (exit)
  224.           )
  225.           ((= d_item 0)
  226.             (princ)
  227.           )
  228.           (T
  229.             (if xld 
  230.               (setq j:xa (cadr d_item))
  231.               (setq j:a (cadr d_item))
  232.             )
  233.             (ll_lox nil)
  234.             (setq ans nil)
  235.           )
  236.         )
  237.       )
  238.     )
  239.   )
  240. )
  241. ;;;
  242. ;;; Read lines from a file until the argument matches the given sub-string
  243. ;;; Returns the last line read as a string.
  244. ;;; ll_rux == LLoad_Read_Until_X
  245. ;;;
  246. (defun ll_rux (str j k / l cont line)
  247.   (setq cont T l 0)
  248.   (while cont
  249.     (setq line (read-line deffi))
  250.     ;;
  251.     ;; Seek to the start of the default file definition
  252.     ;;
  253.     (if line
  254.       (if (= (substr line j k) str)
  255.         (setq cont nil)
  256.         (setq l (1+ l))
  257.       )
  258.       (progn
  259.         (setq cont nil)
  260.       )
  261.     )
  262.   )
  263.   line                                ; Return line as a string
  264. )
  265. ;;;
  266. ;;; List names on the screen until an end of list marker is found.
  267. ;;; Store the items found into a list, I_LIST, a global
  268. ;;; Ignore blank lines and commented lines. Return number of lines.
  269. ;;; ll_lns == LLoad_List_Names_on_Screen
  270. ;;;
  271. (defun ll_lns (str j k / l cont line)
  272.   (setq cont T l 0)
  273.   (while cont
  274.     (if (setq line (read-line deffi))
  275.       ;; Seek to the end of the section delimited by "str"
  276.       ;; Else print the line to the screen preceded by an integer
  277.       (if (= (substr line j k) str)
  278.         (setq cont nil)
  279.         (progn
  280.           (setq l         (1+ l)
  281.                 item      (ll_tok line)
  282.                 I_LIST (if I_LIST
  283.                             (append I_LIST (list item))
  284.                             (list item)
  285.                           )
  286.           )
  287.           (if (and (> l 1) (= (rem l 10) 1))
  288.             (if (= (rem l 20) 1)
  289.               (progn
  290.                 (princ "\n\t<more> ")
  291.                 (grread)
  292.                 (repeat 8 (progn (princ (chr 8))   ; back one char
  293.                                  (princ (chr 32))  ; space
  294.                                  (princ (chr 8)))) ; back one char
  295.               )
  296.               (terpri)
  297.             )
  298.           )
  299.           (princ (strcat "\n\t" (itoa l) ":\t " line))
  300.         )
  301.       )
  302.       (setq cont nil)
  303.     )
  304.   )
  305.   l
  306. )
  307. ;;;
  308. ;;; Tokenize the line, removing any trailing blanks.
  309. ;;; Return the tokenized string
  310. ;;; ll_tok == LLoad_TOKenize
  311. ;;;
  312. (defun ll_tok (str / sl j)
  313.   (setq sl (strlen str)
  314.         j  0
  315.   )
  316.   (while (= (substr str (- sl j) 1) " ")
  317.     (setq j (1+ j))
  318.   )
  319.   (substr str 1 (- sl j))
  320. )
  321. ;;;
  322. ;;; Pick from the list by typing an integer, returns the item, zero or nil.
  323. ;;; ll_pfl == LLoad_Pick_From_List
  324. ;;;
  325. (defun ll_pfl (max_l ig_b ig_str prmpt / OK ans return)
  326.   (while (null OK)
  327.     (initget ig_b ig_str)
  328.     (setq ans (getint prmpt))
  329.     (cond 
  330.       ((= ans "Remove")
  331.         (setq str "\n\tNumber of entry to remove from list: ")
  332.         (setq d_item (ll_pfl max_ls 6 "" str))
  333.         (if (/= d_item nil)
  334.           (progn
  335.             (princ (strcat "\n\tRemoving " (cadr d_item) " from list. "))
  336.             (ll_chl d_item nil)
  337.           )
  338.         )
  339.         (setq OK T return 0)
  340.       )
  341.       ((= ans "Add")
  342.         (setq d_item (list 0 (ll_lox T)))
  343.         (if (nth 1 d_item) (ll_chl d_item T))
  344.         (setq OK     T
  345.               return 0
  346.         ) 
  347.       )
  348.       ((or (= ans "") (null ans))
  349.         (setq OK     T
  350.               return nil
  351.         ) 
  352.       )
  353.       (T
  354.         (cond
  355.           ((and (> ans 0) (<= ans max_l))
  356.             (setq return (list ans (nth (1- ans) I_LIST))
  357.                   OK     T
  358.             )
  359.           )
  360.           (T
  361.             (cond 
  362.               ((= max_l 0)
  363.                 (princ "\n\tNo files to load.")
  364.                 (setq OK nil)
  365.               )
  366.               ((= max_l 1)
  367.                 (princ "\n\tOnly one file to load.")
  368.                 (setq OK nil)
  369.               )
  370.               (T
  371.                 (princ (strcat 
  372.                   "\n\tNumber must be between 1 and " (itoa max_l) "."))
  373.                 (setq OK nil)
  374.               )
  375.             )
  376.           )
  377.         )
  378.       )
  379.     )
  380.   )
  381.   return
  382. )
  383. ;;;
  384. ;;; Load or Xload the selected file.  Returns a file name.
  385. ;;; ll_lox == LLoad_Load_Or_Xload
  386. ;;;
  387. (defun ll_lox (typeit / dflt ans lfile temp)  
  388.   (if typeit
  389.     (progn
  390.       (if (null (if xld j:xa j:a))
  391.         (setq dflt "No default")
  392.         (setq dflt (if xld j:xa j:a))
  393.       )
  394.       (setq ans (getstring (strcat 
  395.         "\n\t" (if xld
  396.                  "External program"
  397.                  "Lisp routine"
  398.                )
  399.                " name to "
  400.                (if unload "un" "")
  401.                "load <" 
  402.                dflt ">: \n\t")))
  403.                
  404.       (if (not (or (eq ans "") (eq ans nil)))
  405.         (progn
  406.           (if (and (> (strlen ans) 4)
  407.                    (= (substr ans (- (strlen ans) 3)) ".lsp"))
  408.             (setq ans (substr ans 1 (- (strlen ans) 4)))
  409.           )
  410.           (set (if xld (read "j:xa") (read "j:a")) ans)
  411.         )
  412.       )
  413.       (if (= (if xld j:xa j:a) "No default")
  414.         (princ "\nNo file specified. ")
  415.       )
  416.     )
  417.   )
  418.   (setq lfile (if xld j:xa (strcat j:a ".lsp")))
  419.   (if (not (setq temp (open lfile "r")))
  420.     (progn
  421.       (setq lfile (findfile (if xld j:xa (strcat j:a ".lsp"))))
  422.     )
  423.     ;else just read it directly from the given path
  424.     (setq temp (close temp))
  425.   )
  426.   (if lfile
  427.     (progn
  428.       (if unload (princ "\n\tUnloading ") (princ "\n\tLoading "))
  429.       (princ (if xld j:xa (strcat j:a ".lsp... ")))
  430.       (if xld
  431.         (if unload 
  432.           (xunload j:xa)
  433.           (xload j:xa)
  434.         )
  435.         (load j:a)
  436.       )
  437.       (princ " Done. ")
  438.     )
  439.     (progn
  440.       (princ "\n\t")
  441.       (princ (if xld j:xa (strcat j:a ".lsp ")))
  442.       (princ " -- Invalid filename or file not found.\n")
  443.       (setq lfile nil)
  444.     )
  445.   )
  446.   (if lfile (if xld j:xa j:a) nil)
  447. )
  448. ;;;
  449. ;;; Add or remove the item from the default file.
  450. ;;; If A_OR_R is T then add, else remove
  451. ;;; ll_chl == LLoad_CHange_List
  452. ;;;
  453. (defun ll_chl (item a_or_r / deffi k temp1 temp2 temp3)
  454.   (if a_or_r
  455.     ;;
  456.     ;; Adding an item to the default list.
  457.     ;;
  458.     (progn
  459.       (if xld
  460.         (setq deffi (ll_lfx ll_xlf "a"))
  461.         (setq deffi (ll_lfx ll_llf "a"))
  462.       )
  463.       (princ (strcat "\n\tWriting " (cadr item) " to default file. "))
  464.       (write-line (cadr item) deffi)
  465.     )
  466.     ;;
  467.     ;; Removing an item from the default list.
  468.     ;;
  469.     (progn
  470.       (if xld
  471.         (setq deffi (ll_lfx ll_xlf "r"))
  472.         (setq deffi (ll_lfx ll_llf "r"))
  473.       )
  474.       (setq temp1 (read-line deffi))
  475.       (setq temp2 (read-line deffi))
  476.       (setq temp3 (read-line deffi))
  477.       (close deffi)
  478.  
  479.       (if xld
  480.         (setq deffi (ll_lfx ll_xlf "w"))
  481.         (setq deffi (ll_lfx ll_llf "w"))
  482.       )
  483.  
  484.       (write-line temp1 deffi)
  485.       (write-line temp2 deffi)
  486.       (write-line temp3 deffi)
  487.       (setq k 0 l (length I_LIST))
  488.  
  489.       (while (and (< k l) (/= k (1- (car item))))
  490.         (write-line (nth k I_LIST) deffi)
  491.         (setq k (1+ k))
  492.       )
  493.       (while (< (setq k (1+ k)) l)
  494.         (write-line (nth k I_LIST) deffi)
  495.       )
  496.     )
  497.   )
  498.   (setq deffi (close deffi))
  499. )
  500. ;;;
  501. ;;; Build the default file from this file.
  502. ;;; ll_bdf == LLoad_Build_Default_File
  503. ;;;
  504. (defun ll_bdf (/ ans deffi)
  505.       
  506.   (initget "Yes No")
  507.   (setq ans (getkword "\nBuild a new default file?  <Y>: "))
  508.   (if (= ans "No")
  509.     (ll_lox T)
  510.     (progn
  511.       (if xld
  512.         (if (setq deffi (open (strcat ll_xpf ll_xlf) "w"))
  513.           (progn
  514.             (princ ";;; Do NOT erase or change the first three lines\n" deffi)
  515.             (princ (strcat ";;; Version " ll_ver 
  516.                            " -- (c) Autodesk, Inc  February 1990\n") deffi)
  517.             (princ ";;; XLOAD Default Files\n" deffi)
  518.             (if j:a (write-line j:a deffi))
  519.             (setq deffi (close deffi))
  520.           )
  521.           (princ "\nError opening XLOAD.DFS for writing. ")
  522.         )
  523.         (if (setq deffi (open (strcat ll_xpf ll_llf) "w"))
  524.           (progn
  525.             (princ ";;; Do NOT erase or change the first three lines\n" deffi)
  526.             (princ (strcat ";;; Version " ll_ver 
  527.                            " -- (c) Autodesk, Inc  February 1990\n") deffi)
  528.             (princ ";;; LISP Default Files\n" deffi)
  529.             (if j:a (write-line j:a deffi))
  530.             (setq deffi (close deffi))
  531.           )
  532.           (princ "\nError opening LISP.DFS for writing. ")
  533.         )
  534.       )
  535.     )
  536.   )
  537. )
  538. ;;;
  539. ;;; Return the first path in ACADPREFIX delimited by ";".
  540. ;;;
  541. ;;; ll_cpf == LLoad_Check_acadPreFix
  542. ;;;
  543. (defun ll_cpf (pf / temp)
  544.   (setq j 1
  545.         l (strlen pf)
  546.   )
  547.   (while (<= j l)
  548.     (if (= (substr pf j 1) ";")
  549.       (progn
  550.         (setq temp (substr pf 1 (1- j)))
  551.         (setq j (1+ l))
  552.       )
  553.       (setq j (1+ j))
  554.     )
  555.   )
  556.   (if temp
  557.     temp
  558.     pf
  559.   )
  560. )
  561. ;;;
  562. ;;; These are the C: function definitions
  563. ;;;
  564. (defun c:ll () (l_load nil nil))
  565. (defun c:xl () (l_load T nil))
  566. (defun c:xul () (l_load T T))
  567. ;;; (defun c:load () (l_load nil nil))
  568. ;;; (defun c:xload () (l_load T nil))
  569. ;;; (defun c:xunload () (l_load T T))
  570. (princ "\n\tLLoad loaded.  Type LL, XL or XUL to start program. \t")
  571. (princ)
  572.